package cn.itcast.contorller.cargo;

import cn.itcast.base.BaseContorller;
import cn.itcast.domain.cargo.*;
import cn.itcast.domain.vo.ExportProductVo;
import cn.itcast.domain.vo.ExportResult;
import cn.itcast.domain.vo.ExportVo;
import cn.itcast.service.ContractService;
import cn.itcast.service.ExportProductService;
import cn.itcast.service.ExportService;
import cn.itcast.utils.UtilFuns;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.cxf.jaxrs.client.WebClient;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Controller
    @RequestMapping(value = "/cargo/export")
    public class exportController extends BaseContorller {
        @Reference
        private ContractService contractService;//合同
        @Reference
        private ExportService exportService;//出口保运
        @Reference
        private ExportProductService exportProductService;
        @RequestMapping(value = "/contractList",name ="分页查询合同管理")
      public String findAll(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize){
            ContractExample contractExample=new ContractExample();
            contractExample.setOrderByClause("create_time desc");
            ContractExample.Criteria criteria = contractExample.createCriteria();
            criteria.andCompanyIdEqualTo(company_id);
            /*查询的是以上报的合同*/
            criteria.andStateEqualTo(1); //合同已上报的
            PageInfo all =contractService.findAll(contractExample,pageNum,pageSize);
            request.setAttribute("pb",all);
            return "/cargo/export/export-contractList";
        }
        @RequestMapping(value = "/list",name = "生成报运单")
        public String list(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize){
            ExportExample exportExample=new ExportExample();
            exportExample.setOrderByClause("create_time desc");
            ExportExample.Criteria criteria = exportExample.createCriteria();
            criteria.andCompanyIdEqualTo(company_id);
            PageInfo all = exportService.findAll(exportExample, pageNum, pageSize);
            request.setAttribute("pb",all);
            return "/cargo/export/export-list";
        }
        @RequestMapping(value = "/toExport",name = "到报运单生成页面")
        public String toExport(String id){
            //复选框多个值：String[]
            //复选框多个值：String  例如：“1,2”
            request.setAttribute("id",id);
            return "/cargo/export/export-toExport";
        }
        @RequestMapping(value = "/edit",name = "报运单的生成")
        public String edit(Export export){
            export.setCompanyId(company_id);
            export.setCompanyName(company_name);
            if (UtilFuns.isEmpty(export.getId())){
                /*新增报运单*/
            exportService.save(export);
            }else {
                System.out.println(export);
                /*修改报运单*/
                exportService.update(export);
            }
            return "redirect:/cargo/export/list.do";
        }
        @RequestMapping(value = "/toView",name = "查看报运单")
        public String toView(String id){
            Export byId = exportService.findById(id);
            request.setAttribute("export",byId);
            return "/cargo/export/export-view";
        }

    @RequestMapping(value = "/toUpdate", name = "到报运单修改页面回显")
    public String toUpdate(String id) {
            /*回显 报运单*/
        Export export = exportService.findById(id);
        request.setAttribute("export", export);
        /*回显包运单下的货物*/
        ExportProductExample exportProductExample = new ExportProductExample();
        ExportProductExample.Criteria criteria = exportProductExample.createCriteria();
        criteria.andExportIdEqualTo(id);
        List<ExportProduct> all = exportProductService.findAll(exportProductExample);
        request.setAttribute("eps",all);
        return "/cargo/export/export-update";
    }
    @RequestMapping(value = "/delete",name = "删除报运单")
    public String delete(String id){
        exportService.delete(id);
        return "redirect:/cargo/export/list.do";
    }
    @RequestMapping(value ="/submit",name = "提交报运单")
    public String submit(String id){
        Export export = exportService.findById(id);
        if (export.getState()==0){
            export.setState(1);
            exportService.update(export);
        }
        return "redirect:/cargo/export/list.do";
    }
    @RequestMapping(value = "/cancel",name = "取消报运单")
    public String cancel(String id){
        Export export = exportService.findById(id);
        if (export.getState()==1){
            export.setState(0);
            exportService.update(export);
        }
        return "redirect:/cargo/export/list.do";
        }
        @RequestMapping(value ="/exportE",name = "电子报运提交给海关")
        public  String exportE(String id){
            //1 先报运
            //1.1 根据报运单的id查询到报运单对象
            Export export = exportService.findById(id);
            //1.2 将报运单的对象数据封装给海关需要的ExportVo对象(报运单数据 报运单货物数据)
            ExportVo exportVo = new ExportVo();
            BeanUtils.copyProperties(export,exportVo);
            exportVo.setExportId(export.getId());

            //1.3 将报运单的货物对象数据给海关需要的ExportProductVo对象
            ExportProductExample exportProductExample = new ExportProductExample();
            ExportProductExample.Criteria criteria = exportProductExample.createCriteria();
            /*根据报运单id 查的货物*/
            criteria.andExportIdEqualTo(id);
            List<ExportProduct> exportProductList = exportProductService.findAll(exportProductExample);
            /*给海关需要的货物 参数 封装数据*/
            List<ExportProductVo> list=new ArrayList<>();
            for (ExportProduct exportProduct : exportProductList) {
                //有一个exportProduct对象就得有一个海关需要的exportProductVo对象
                ExportProductVo exportProductVo = new ExportProductVo();
                BeanUtils.copyProperties(exportProduct,exportProductVo);
                exportProductVo.setExportProductId(exportProduct.getId());
                list.add(exportProductVo);
            }
            //1.4 将封装好的exportProductVo集合给exportVo
            exportVo.setProducts(list);
            //1.5 通过cxf的api实现远程服务的调用
            WebClient webClient = WebClient.create("http://localhost:8082/jk/ws/export/user");
            webClient.post(exportVo);

            WebClient webClient2=webClient.create("http://localhost:8082/jk/ws/export/user/"+id);
            ExportResult exportResult = webClient2.get(ExportResult.class);
            // 根据结果修改自己数据
            exportService.updateExportAndExportProduct(exportResult);
            return "redirect:/cargo/export/list.do";
        }



    }
